package com.jiangore.client;

import org.apache.cxf.binding.soap.SoapHeader;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.namespace.QName;
import java.util.List;

/**
 * @Todo
 * @Author jiangtao
 * @CreateBy 2020-03-14 11:18 PM
 * @UpdateBy jiangtao
 * @Version v1.0
 */
public class ClientLoginInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

    private String token;

    public ClientLoginInterceptor(String token) {
        super(Phase.PREPARE_SEND);
        this.token = token;
    }

    @Override
    public void handleMessage(SoapMessage soapMessage) throws Fault {
        List<Header> headers = soapMessage.getHeaders();

        Document doc = DOMUtils.createDocument();
        Element auth = doc.createElement("header");
        Element token = doc.createElement("token");
        token.setTextContent(this.token);
        auth.appendChild(token);

        Header header = new SoapHeader(new QName("header"), auth);
        headers.add(header);
    }
}
